import { MonitoredTokenService } from '@/services/token/MonitoredTokenService';
import { createModuleLogger } from '@/utils/logger';

const logger = createModuleLogger('price-service');

export class PriceService {
  private static instance: PriceService;
  private monitoredTokenService: MonitoredTokenService;

  private constructor() {
    this.monitoredTokenService = MonitoredTokenService.getInstance();
  }

  public static getInstance(): PriceService {
    if (!PriceService.instance) {
      PriceService.instance = new PriceService();
    }
    return PriceService.instance;
  }

  // 初始化服务
  public async initialize(): Promise<void> {
    logger.info('✅ 价格服务已初始化 (使用数据库存储)');
      }

  // 获取代币价格 - 直接从数据库获取
  public async getTokenPrice(symbol: string): Promise<number> {
    return await this.monitoredTokenService.getTokenPrice(symbol);
      }

  // 获取ETH价格
  public async getETHPrice(): Promise<number> {
    return await this.getTokenPrice('ETH');
  }

  // 获取BTC价格
  public async getBTCPrice(): Promise<number> {
    return await this.getTokenPrice('BTC');
  }

  // 获取BNB价格
  public async getBNBPrice(): Promise<number> {
    return await this.getTokenPrice('BNB');
  }

  // 检查代币是否在监控列表中
  public async isTokenMonitored(symbol: string): Promise<boolean> {
    return await this.monitoredTokenService.isTokenMonitored(symbol);
  }

  // 检查代币是否支持（向后兼容）
  public isTokenSupported(symbol: string): boolean {
    // 简化实现，只检查常见代币
    const supportedTokens = ['BTC', 'ETH', 'BNB', 'SOL', 'ADA', 'DOT', 'MATIC', 'AVAX', 'LINK', 'UNI', 'AAVE'];
    return supportedTokens.includes(symbol.toUpperCase());
  }

  // 获取白名单统计（向后兼容）
  public getWhitelistStats(): {
    totalSupported: number;
    lastUpdate: Date | null;
    nextUpdate: Date | null;
  } {
    return {
      totalSupported: 1000, // 假设支持1000个代币
      lastUpdate: new Date(),
      nextUpdate: null
    };
  }

  // 批量获取多个代币价格
  public async getBatchTokenPrices(symbols: string[]): Promise<Map<string, number>> {
    const results = new Map<string, number>();
    
    for (const symbol of symbols) {
        try {
          const price = await this.getTokenPrice(symbol);
        if (price > 0) {
          results.set(symbol.toUpperCase(), price);
        }
      } catch (error) {
        logger.debug(`获取 ${symbol} 价格失败:`, error);
      }
    }

    return results;
  }

  // 启用WebSocket（向后兼容，实际不使用）
  public enableWebSocket(): void {
    logger.info('📊 价格服务使用数据库模式，无需WebSocket');
  }

  // 检查WebSocket是否启用（向后兼容）
  public isWebSocketEnabled(): boolean {
    return false; // 不再使用WebSocket
  }
} 